package com.inei.reportedeverificacion.view;

import java.util.ArrayList;
import java.util.List;
import com.inei.reportedeverificacion.R;
import com.inei.reportedeverificacion.controller.ReporteCap11Controller;
import com.inei.reportedeverificacion.controller.ReporteController;
import com.inei.reportedeverificacion.model.entity.Reporte;
import com.inei.reportedeverificacion.model.entity.ReporteCap11;
import com.inei.reportedeverificacion.model.entity.ReporteCap11Tmp;
import com.inei.reportedeverificacion.model.entity.ReporteTmp;
import com.inei.reportedeverificacion.tools.HolderCap11;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

public class Fragment_Seccion_3 extends Activity {

	// componentes
	private TextView tvC9_109, tvC9_118, tvC9_132, tvC10_142,
			tvC10_142_Cuantos;
	private Spinner spC9_109, spC9_118, spC9_132, spC10_142, spC10_142_Cuantos;
	private LinearLayout llC11_1;
	public List<Spinner> spinnerCap11_1, spinnerCap11_2;

	// variables
	private ReporteController con;
	private Reporte rep;
	private ReporteTmp repTmp;
	private ReporteCap11Controller con11;
	private List<ReporteCap11> cap11;
	private List<ReporteCap11Tmp> cap11Tmp;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.fragment_seccion_3);

		con = new ReporteController(getApplicationContext());
		con11 = new ReporteCap11Controller(getApplicationContext());

		IniciarComponentes();

		CargarDatos();
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.menu_frag3, menu);
		return super.onCreateOptionsMenu(menu);
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {

		switch (item.getItemId()) {
		case R.id.mnGuardar:
			GuardarDatos();
			break;
		case R.id.mnSalir:
			finish();
			break;
		default:
			break;
		}
		return super.onOptionsItemSelected(item);
	}

	@Override
	public void onBackPressed() {
		Intent ir_fra2 = new Intent(this, Fragment_Seccion_2.class);
		ir_fra2.putExtras(getIntent().getExtras());
		startActivity(ir_fra2);
		finish();

		super.onBackPressed();
	}

	private void IniciarComponentes() {
		tvC9_109 = (TextView) findViewById(R.id.tvC9_109);
		tvC9_118 = (TextView) findViewById(R.id.tvC9_118);
		tvC9_132 = (TextView) findViewById(R.id.tvC9_132);
		tvC10_142 = (TextView) findViewById(R.id.tvC10_142);
		tvC10_142_Cuantos = (TextView) findViewById(R.id.tvC10_142_Cuantos);

		spC9_109 = (Spinner) findViewById(R.id.spC9_109);
		spC9_118 = (Spinner) findViewById(R.id.spC9_118);
		spC9_132 = (Spinner) findViewById(R.id.spC9_132);
		spC10_142 = (Spinner) findViewById(R.id.spC10_142);
		spC10_142_Cuantos = (Spinner) findViewById(R.id.spC10_142_Cuantos);

		llC11_1 = (LinearLayout) findViewById(R.id.llC11_1);
	}

	private void CargarDatos() {
		repTmp = new ReporteTmp();
		repTmp.setCLAVEVIVHOGAR(getIntent().getExtras().getString(
				"CLAVEVIVHOGAR"));
		repTmp = con.ReporteTmpBuscar(repTmp);

		if (repTmp != null) {
			if (repTmp.getC1P109() != 0) {
				tvC9_109.setText(rptaSiNo(repTmp.getC1P109()));
			}
			if (repTmp.getC1P118() != 0) {
				tvC9_118.setText(rptaSiNo(repTmp.getC1P118()));
			}
			if (repTmp.getC1P132() != 0) {
				tvC9_132.setText(rptaSiNo(repTmp.getC1P132()));
			}
			if (repTmp.getC1P142() != 0) {
				tvC10_142.setText(rptaSiNo(repTmp.getC1P142()));
			}
			if (repTmp.getT_05_DIG_CAP10_2() != 0) {
				tvC10_142_Cuantos.setText(repTmp.getT_05_DIG_CAP10_2() + "");
			}
		}

		rep = new Reporte();
		rep.setCLAVEVIVHOGAR(getIntent().getExtras().getString("CLAVEVIVHOGAR"));
		rep = con.ReporteBuscar(rep);

		if (rep != null) {
			if (rep.getC1P109() != 0) {
				spC9_109.setSelection(rep.getC1P109());
			}
			if (rep.getC1P118() != 0) {
				spC9_118.setSelection(rep.getC1P118());
			}
			if (rep.getC1P132() != 0) {
				spC9_132.setSelection(rep.getC1P132());
			}
			if (rep.getC1P142() != 0) {
				spC10_142.setSelection(rep.getC1P142());
			}
			if (rep.getC1P142_COUNT() != 0) {
				spC10_142_Cuantos.setSelection(rep.getC1P142_COUNT());
			}
		}

		cap11Tmp = con11.ReporteCap11TmpLeerXHOGAR(getIntent().getExtras()
				.getString("CLAVEVIVHOGAR"));

		cap11 = con11.ReporteCap11LeerXHOGAR(getIntent().getExtras().getString(
				"CLAVEVIVHOGAR"));

		if (cap11Tmp != null) {
			CargarCap11();
		}
	}

	private void CargarCap11() {
		spinnerCap11_1 = new ArrayList<Spinner>();
		spinnerCap11_2 = new ArrayList<Spinner>();
		if (llC11_1 != null) {
			llC11_1.removeAllViews();
		}
		for (int i = 0; i < cap11Tmp.size(); i++) {
			View item = null;
			LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			item = inflater.inflate(R.layout.cap11_item, null);
			ReporteCap11Tmp obj = cap11Tmp.get(i);
			HolderCap11 holder = new HolderCap11();
			holder.tvcap111 = (TextView) item.findViewById(R.id.tvcap111);
			holder.tvcap112 = (TextView) item.findViewById(R.id.tvcap112);
			holder.spcap113 = (Spinner) item.findViewById(R.id.spcap113);
			holder.tvcap114 = (TextView) item.findViewById(R.id.tvcap114);
			holder.spcap115 = (Spinner) item.findViewById(R.id.spcap115);

			holder.tvcap111.setText((obj.getC1P21A() + " " + obj.getC1P21B()
					+ " " + obj.getC1P21C()));
			holder.tvcap112.setText(rptaSiNo(obj.getC1P158()));
			holder.tvcap114.setText(rptaSiNo(obj.getC1P173()));

			if (cap11 != null) {
				if (cap11.get(i).getC1P158() != 0) {
					holder.spcap113.setSelection(cap11.get(i).getC1P158());
				}
				if (cap11.get(i).getC1P173() != 0) {
					holder.spcap115.setSelection(cap11.get(i).getC1P173());
				}
			}

			spinnerCap11_1.add(holder.spcap113);
			spinnerCap11_2.add(holder.spcap115);
			llC11_1.addView(item);
		}
	}

	public void GuardarDatos() {
		rep.setC1P109(spC9_109.getSelectedItemPosition());
		rep.setC1P118(spC9_118.getSelectedItemPosition());
		rep.setC1P132(spC9_132.getSelectedItemPosition());
		rep.setC1P142(spC10_142.getSelectedItemPosition());
		rep.setC1P142_COUNT(spC10_142_Cuantos.getSelectedItemPosition());

		con.ReporteModificar(rep);
		if (cap11Tmp != null) {
			int n = 0;
			if (cap11 == null) {
				cap11 = new ArrayList<ReporteCap11>();
				for (int i = 0; i < cap11Tmp.size(); i++) {
					ReporteCap11 r = new ReporteCap11();
					r.setCLAVEVIVHOGAR(getIntent().getExtras().getString(
							"CLAVEVIVHOGAR"));
					r.setNRO_ORDEN(cap11Tmp.get(i).getNRO_ORDEN());
					cap11.add(r);
				}
				n = 1;
			}
			for (int i = 0; i < cap11Tmp.size(); i++) {
				cap11.get(i).setC1P158(
						spinnerCap11_1.get(i).getSelectedItemPosition());
				cap11.get(i).setC1P173(
						spinnerCap11_2.get(i).getSelectedItemPosition());
				if (n == 1) {
					if (con11.ReporteCap11Insertar(cap11.get(i)) > 0) {
						Intent ir_frag4 = new Intent(this,
								Fragment_Seccion_4.class);
						Bundle datos = getIntent().getExtras();
						ir_frag4.putExtras(datos);
						startActivity(ir_frag4);

						Toast.makeText(this, "Se guardo con exito",
								Toast.LENGTH_SHORT).show();

						finish();
					} else {
						Toast.makeText(this, "Error al guardar",
								Toast.LENGTH_SHORT).show();
					}
				} else {
					if (con11.ReporteCap11Modificar(cap11.get(i)) > 0) {
						Intent ir_frag4 = new Intent(this,
								Fragment_Seccion_4.class);
						Bundle datos = getIntent().getExtras();
						ir_frag4.putExtras(datos);
						startActivity(ir_frag4);

						Toast.makeText(this, "Se actualizo con exito",
								Toast.LENGTH_SHORT).show();

						finish();
					} else {
						Toast.makeText(this, "Error al actualizar",
								Toast.LENGTH_SHORT).show();
					}
				}
			}
		}else{
			Intent ir_frag4 = new Intent(this,
					Fragment_Seccion_4.class);
			Bundle datos = getIntent().getExtras();
			ir_frag4.putExtras(datos);
			startActivity(ir_frag4);
			Toast.makeText(this, "Se actualizo con exito",
					Toast.LENGTH_SHORT).show();
		}
	}

	private String tabla01(int pos) {
		return getResources().getStringArray(R.array.tabla01)[pos];
	}

	private String tabla02(int pos) {
		return getResources().getStringArray(R.array.tabla02)[pos];
	}

	private String tabla03(int pos) {
		return getResources().getStringArray(R.array.tabla03)[pos];
	}

	private String rptaSiNo(int pos) {
		return getResources().getStringArray(R.array.rptaSiNo)[pos];
	}
}
